package org.wlldTest.lesson5;


import org.dromara.easyai.matrixTools.Matrix;
import org.dromara.easyai.matrixTools.MatrixOperation;

public class MyLine {
    private final Matrix feature;
    private final Matrix E;
    private float w1;
    private float w2;
    private float b;
    private int index = 0;
    private final Matrix vec = new Matrix(1, 3, "[1,0,0]#");
    private final MatrixOperation matrixOperation = new MatrixOperation();

    public MyLine(int size) throws Exception {
        feature = new Matrix(size, 3);
        E = new Matrix(size, 1);
    }

    public void insertXY(float x, float y, float value) throws Exception {
        feature.setNub(index, 0, x);
        feature.setNub(index, 1, y);
        feature.setNub(index, 2, 1);
        E.setNub(index, 0, value);
        index++;
    }

    public double getValue(float x, float y) throws Exception {
        return w1 * x + w2 * y + b;
    }

    public float getCos() throws Exception {
        Matrix matrix = new Matrix(1, 3);
        matrix.setNub(0, 0, w1);
        matrix.setNub(0, 1, w2);
        matrix.setNub(0, 2, b);
        return matrixOperation.getNormCos(matrix, vec);
    }

    public void start() throws Exception {//计算 feature * w = E
        Matrix wm = matrixOperation.getLinearRegression(feature, E);
        if (wm.getX() == 1 && wm.getY() == 1) {//该矩阵奇异
            throw new Exception("该矩阵行列式为0");
        } else {
            w1 = wm.getNumber(0, 0);
            w2 = wm.getNumber(1, 0);
            b = wm.getNumber(2, 0);
        }
    }
}
